import time
import unittest

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait


class TestLogin(unittest.TestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.get('https://www.126.com/')
        self.result = ''
        self.target = ''
        self.frame = self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[3]/div[1]/div/div[4]/div[1]/div[1]/iframe')
        self.browser.switch_to.frame(self.frame)

        self.usrname = self.browser.find_element_by_name('email')
        self.pwd = self.browser.find_element_by_name('password')
        self.loginbtn = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[8]/a')

    def test_login01(self):
        self.usrname.send_keys('gench2110153')
        self.pwd.send_keys('Qq1729256881')
        self.loginbtn.click()
        time.sleep(2)
        self.browser.switch_to.default_content()
        WebDriverWait(self.browser, 60, 0.5).until(lambda browser: self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[1]/div/div/div[2]/div[1]/div[2]/span[1]/span[1]'))

        self.target = 'gench2110153'
        self.result = self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[1]/div/div/div[2]/div[1]/div[2]/span[1]/span[1]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_login02(self):
        self.usrname.send_keys('')
        self.pwd.send_keys('Qq1729256881')
        self.loginbtn.click()
        WebDriverWait(self.browser, 60, 0.5).until(lambda browser: self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]'))
        self.target = '请输入帐号'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_login03(self):
        self.usrname.send_keys('testgench2110153')
        self.pwd.send_keys('Qq1729256881')
        self.loginbtn.click()
        time.sleep(1)
        info = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]').text
        if info == '请先进行验证':
            time.sleep(20)
        WebDriverWait(self.browser, 60, 0.5).until(lambda browser: self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]'))
        self.target = '帐号或密码错误'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_login04(self):
        self.usrname.send_keys('gench2110153')
        self.pwd.send_keys('')
        self.loginbtn.click()
        time.sleep(1)
        info = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]').text
        if info == '请先进行验证':
            time.sleep(20)
        WebDriverWait(self.browser, 60, 0.5).until(lambda browser: self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]'))
        self.target = '请输入密码'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_login05(self):
        self.usrname.send_keys('gench2110153')
        self.pwd.send_keys('gench2110153')
        self.loginbtn.click()
        time.sleep(1)
        info = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]').text
        if info == '请先进行验证':
            time.sleep(20)
        WebDriverWait(self.browser, 60, 0.5).until(lambda browser: self.browser.find_element_by_xpath(
            '/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]'))
        self.target = '帐号或密码错误'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/form/div/div[7]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def tearDown(self):
        self.browser.close()


if __name__ == '__main__':
    unittest.main()
